#include <iostream>
#include <stdio.h>

#include "Graph.h"

using namespace std;

int Graph::Weight(int v1, int v2) {
	for (auto &r : m_ve) {
		if (r.v1 == v1 && r.v2 == v2)
			return r.w;
		if  (r.v2 == v1 && r.v1 == v2)
			return r.w;
	}
	return 0;
}

ArrGraph::ArrGraph(int v)
	: Graph(v), vexs(v, 0) {
	for (int i = 0; i < v; i++) {
		arcs.push_back(vector<int>(v, 0));
	}
}

bool ArrGraph::AddEdge(int v1, int v2, int w) {
	if ( (v1 < 0 || v1 > V()) || (v2 < 0 || v2 > V()) ) {		//工具库 InRange(int, int)
		printf("AddEdge %d-%d failed: param overlimit %d\n", v1, v2, V());	//日志库
		return false;
	}
	arcs[v1][v2] = 1;
	arcs[v1][v2] = 1;
	m_ne++;
	m_ve.push_back(Edge{v1, v2, w});
	return true;
}

void ArrGraph::Show() {
	cout << setw(5) << " ";
	for (int j = 0; j < V(); j++) {
		cout << setw(5) << j;
	}
	cout << endl;
	
	for (int i = 0; i < V(); i++) {
		cout << setw(5) << i;
		for (int j = 0; j < V(); j++) {
			cout << setw(5) << arcs[i][j];
		}
		cout << endl;
	}
}

vector<int> ArrGraph::Adj(int v) {
	vector<int> ret;
	for (int i = 0; i < V(); i++) {
		if ( arcs[v][i] == 1 )
			ret.push_back(i);
	}
	
	return ret;
}

int ArrGraph::IndexVex(int ver) {
	return 0;
}

ListGraph::ListGraph(int v) 
	: Graph(v) {
    for (int i = 0; i < v; i++) {
		list<int> l;
		//l.push_back(i);
		m_vAdj.push_back(l);
	}
}

int ListGraph::IndexVex(int ver) {
	return 0;
}

bool ListGraph::AddEdge(int v1, int v2, int w) {
	//TODO:v,w值合法判断
	m_vAdj[v1].push_back(v2);
	m_vAdj[v2].push_back(v1);
	m_ne++;
	m_ve.push_back(Edge{v1, v2, w});
 	return true;
}

vector<int> ListGraph::Adj(int v) {
	vector<int> ret;
	for (auto &r : m_vAdj[v]) {
		ret.push_back(r);
	}
	return ret;
}

void ListGraph::Show() {
	for (auto &r : m_vAdj) {
		for (auto &i : r) {
			cout << setw(5) << i;
		}
		cout << endl;
	}
}

Digraph::Digraph(int v)
	: ArrGraph(v) {

}

bool Digraph::AddEdge(int ver) {

}

vector<int> Digraph::Adj(int v) {

}
